local DB = require("common.DB")
local FamilyDefine = require("family.FamilyDefine")
local Human = require("core.Human")
local RoleDBLogic = require("role.RoleDBLogic")
local FamilyExcel = require("excel.Family")
local OfficialExcel = FamilyExcel.official
local FamilyLogicFW = require("family.FamilyLogicFW")
local BuildingExcel = FamilyExcel.building
local Util = require("common.Util")
local Msg = require("core.Msg")
local LuaMongo = _G.lua_mongo
local RoleFFI = require("role.RoleFFI")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local RankHandler = require("rank.HandlerW")
local CopyCallbackW = require("copy.CallbackW")
local Config = require("Config")


uuid2Family = uuid2Family or {}
familyName2Family = familyName2Family or {}

local DBUpdate = { _id = nil}
local DBRemove = {_id = nil}

--取军团 by uuid
function findByUuid(familyUuid)
    return uuid2Family[familyUuid]
end

--取军团 by 军团名
function findByFamilyName(name)
    return familyName2Family[name]
end

-- 取主公
function getLeader(family)
    for k, v in ipairs(family.memberList) do
        if v.official == FamilyDefine.OFFICIAL_LEADER then
            return v
        end
    end
end

-- 取同盟
function getAlly(familyUuid)
    if not familyUuid then
        return
    end
    local family = findByUuid(familyUuid)
    if not family.allyUuid then
        return
    end
    return findByUuid(family.allyUuid)
end


-- 取军团名
function getFamilyName(familyUuid)
    local family = findByUuid(familyUuid)
    if not family then
        return
    end
    return family.familyName
end

local f = {
    name=1,
    familyUuid=1,
    zhandouli=1,
    lv=1,
    yinliang=1,
    sex=1,
    equip=1,
}
-- 取军团族长DB
function getLeaderDB(familyUuid)
    local family = findByUuid(familyUuid)
    if not family then
        return
    end
    local leader = getLeader(family)
    if not leader then
        return
    end

    return RoleDBLogic.getDb(leader.uuid, f)
end

--取军团名
function getFamilyNameByUuid(familyUuid)
    local family = findByUuid(familyUuid)
    if not family then
        return ""
    end
    return family.familyName
end

-- 取官职数量
function getOfficialCnt(family, official)
    local cnt = 0
    for _, memeber in ipairs(family.memberList) do
        if memeber.official == official then
            cnt = cnt + 1
        end
    end
    return cnt
end

--取成员
function getMember(family, targetUuid)
    for _, member in ipairs(family.memberList) do
        if member.uuid == targetUuid then
            return member
        end
    end
end

--取成员官职
function getMemberOfficial(familyUuid, targetUuid)
    local family = uuid2Family[familyUuid]
    if family then
        for _, member in ipairs(family.memberList) do
            if member.uuid == targetUuid then
                return member.official
            end
        end
    else
        return nil
    end
end

--根据官职取成员
function getMemberByOfficial(family, official)
    for _, member in ipairs(family.memberList) do
        if member.official == official then
            return member
        end
    end
end

--判断是否在某官职
function isOfficial(family, official, uuid)
    local member = getMember(family, uuid)
    if not member then
        return
    end

    if member.official ~= official then
        return
    end
    return true
end

--取战斗力
function getZhandouli(family)
    local zhandouli = 0
    local onlineUuid = Human.onlineUuid
    for _, member in ipairs(family.memberList) do
        local target = onlineUuid[member.uuid]
        if target then
            member.zhandouli = target.db.zhandouli
        end
        zhandouli = zhandouli + member.zhandouli
    end
    return zhandouli
end

--改变官职
function setOfficial(family, targetUuid, official)
    local member = getMember(family, targetUuid)
    if not member then
        assert()
    end
    member.official = official
    update(family)
end

--改变成员贡献值
function setContribution(human, addValue)
    local family = findByUuid(human.familyUuid)
    if not family then
        assert()
    end
    local member = getMember(family, human._id)
    if not member then
        assert()
    end
    member.familyContribution = human.db.familyContribution
    if not member.familyContributionSum then
        member.familyContributionSum = 0
    end
    if addValue > 0 then
        member.familyContributionSum = member.familyContributionSum + addValue
    end
    update(family)
end

--判断是否在申请列表中
function isApplyed(family, uuid)
    if not family.applyList or not family.applyList[uuid] then
        return false
    end
    local apply = family.applyList[uuid]
    if not Util.isSameDay(apply.applyTime) then
        delApply(family, uuid)
        return false
    end
    return true
end

-- 增加申请列表
function addApply(family, human)
    if not family._id then
        assert()
    end

    if not family.applyList then
        family.applyList = {}
    end

    if not family.applyList[human._id] then
        family.applyList[human._id] = {}
    end

    local now = os.time()


    if not human.db.familyApplyList then
        human.db.familyApplyList = {}
    end
    human.db.familyApplyList[human.db.familyApplyListLen].uuid = family._id
    human.db.familyApplyList[human.db.familyApplyListLen].applyTime =  now

    local apply = family.applyList[human._id]
    apply.uuid = human._id
    apply.name = human.name
    apply.lv = human.db.lv
    apply.sex = human.db.sex
    apply.official = nil
    apply.lastLoginTime = human.db.lastLoginTime
    apply.familyContribution = human.db.familyContribution
    apply.head = human.db.head
    apply.zhandouli = human.db.zhandouli
    apply.applyTime = now
    DBUpdate._id = family._id
    LuaMongo.update(DB.db_family, DBUpdate, family)
    onApplyCntChange(family)
end

-- 删除申请列表
function delApply(family, uuid, reason)
    if not family._id or not family.applyList or not family.applyList[uuid] then
        return
    end

    family.applyList[uuid] = nil
    DBUpdate._id = family._id
    LuaMongo.update(DB.db_family, DBUpdate, family)
    onApplyCntChange(family)

    local target = Human.onlineUuid[uuid]
    if target and target.fd then
        if reason and reason ~= "" then
            local msgErr = Msg.gc.GC_BROADCAST
            msgErr.type = Lang.DOWN
            msgErr.msg = reason
            Msg.send(msgErr, target.fd)
        end

        local msgRet = Msg.gc.GC_FAMILY_APPLY_CANCEL
        msgRet.familyUuid = family._id
        Msg.send(msgRet, target.fd)
    end
    return true
end

-- 删除申请列表s
function delApplys(uuid)
    for _, family in pairs(uuid2Family) do
        delApply(family, uuid)
    end
end

function onApplyCntChange(family)
    local fds = getAuthFds(family, auth1)
    if fds[0] ~= 0 then
        local msgRet = Msg.gc.GC_FAMILY_APPLY_CNT_CHANGE
        msgRet.cnt = getApplyListLen(family)
        Msg.sendMulti(msgRet, fds)
    end
end

auth1 = auth1 or {
    [1] = true,
    [2] = true,
}
-- 取具体权限fds
function getAuthFds(family, auth)
    local fds = Msg.list
    local len = 0
    for _, member in ipairs(family.memberList) do
        local target = Human.onlineUuid[member.uuid]
        if target and target.fd and auth[member.official] then
            len = len + 1
            fds[len] = target.fd
        end
    end
    fds[0] = len
    return fds
end

-- 取军团申请列表长度
function getApplyListLen(family)
    local len = 0
    if not family.applyList then
        return len
    end
    for k in pairs(family.applyList) do
        len = len + 1
    end
    return len
end

--判断是否在邀请列表中
function isInvited(family, targetname)
    if not family.inviteList or not family.inviteList[targetname] then
        return false
    end
    local time = os.time() - family.inviteList[targetname]
    if time > 600 then
        family.inviteList[targetname] = os.time()
        return false
    end
    return true
end

--增加邀请列表
function addInvite(family, targetname)
    if not family._id then
        assert()
    end
    if not family.inviteList then
        family.inviteList = {}
    end
    family.inviteList[targetname] = os.time()
    DBUpdate._id = family._id
    LuaMongo.update(DB.db_family, DBUpdate, family)
    return true
end

-- 删除邀请列表
function delInvite(family, targetname)
    if not family.inviteList or not family._id or not family.inviteList[targetname] then
        return
    end
    family.inviteList[targetname] = nil
    DBUpdate._id = family._id
    LuaMongo.update(DB.db_family, DBUpdate, family)
end

-- 删除邀请列表s
function delInvites(targetname)
    for _, family in pairs(uuid2Family) do
        delInvite(family, targetname)
    end
end

-- 取邀请军团
function getInviteFamily(humanUuid)
    local familys= {}
    for _, family in pairs(uuid2Family) do
        if family.inviteList then
            if family.inviteList[humanUuid] then
                familys[#familys + 1] = family
            end
        end
    end
    return familys
end

-- 取军团邀请列表长度
function getInviteListLen(family)
    local len = 0
    if not family.inviteList then
        return len
    end
    for k in pairs(family.inviteList) do
        len = len + 1
    end
    return len
end
-- 军团数据模板
function familyTemplate()
    local family = {
        familyName = "",                                      --军团名
        familyBanner = 0,                                     --军团旗帜
        FamilyBannerItemData = {},                            --军旗数据
        sign = "",                                            --军团签名
        memberList = {},                                          --成员列表  非稀疏
        familyExp = nil,                                          --军团经验
        familyLv = 0,                                            --军团等级
        applyList = nil,                                          --军团申请列表 k:human uuid
        familyApplyState = 0,                                                                               --军团自动通过申请状态
        inviteList = nil,                                           --军团邀请列表 k:human uuid
        createTime = os.time(),                                           --军团创建时间
        familyMoney = 0,                                          --军团资金
        zhandouli = 0,                                         --战斗力
        activityRate = FamilyDefine.ACTIVITY_RATE,                                                    --活跃度
        activityRateTime = nil,                                         --活跃度增加时间
        store = {},                                             --军团仓库
        storeLen = 0,                                                                                       --军团仓库实际存储长度
        storeRecord = {},                                           --军团仓库记录
        dailyFamilyExpyday = nil,                                       --每天军团经验 yday
        dailyFamilyExp = nil,                                         --每天军团经验值
        officialName = {                                          --军团官职名称自定义
        [FamilyDefine.OFFICIAL_LEADER] = OfficialExcel[FamilyDefine.OFFICIAL_LEADER].name,        --主公
        [FamilyDefine.OFFICIAL_FU_LEADER] = OfficialExcel[FamilyDefine.OFFICIAL_FU_LEADER].name,    --军师
        [FamilyDefine.OFFICIAL_JINGYING] = OfficialExcel[FamilyDefine.OFFICIAL_JINGYING].name,      --上将
        [FamilyDefine.OFFICIAL_NORMAL] = OfficialExcel[FamilyDefine.OFFICIAL_NORMAL].name,        --普通族员
        },
        familyTitle = nil,                                          --军团称号
        familyBounty = nil,                                         --军团悬赏任务
        extendStartTime = nil,                                        --军团继承开始时间
        lastExtendStartTime = nil,                                      --上次军团继承开始时间
        extends = nil,                                            --继承者列表
        extendTipFlag = nil,                                        --继承者提示标识
        allyTargetUuid = nil,                                         --目标同盟uuid
        allyTargetTime = nil,                                         --目标同盟时间
        allySrcUuid = nil,                                          --源同盟uuid
        allySrcTime = nil,                                          --源同盟时间
        monster = nil,                                              --神兽 {lv, exp, attrs, logs, pkID, pkExp, pkStartTime, pkStartID, }
        recruitSign = nil,                                          --招募公告
        tllv = 0,                                                                                           --韬略等级
        familyCopyTimes = 0,                                        --军团已完成副本次数
        middleFamilyWarStatus = 0,                                  -- 0为 未报名，1为已报名, 2争霸中， 3落选
        middleFamilyCanEnter = nil,                                 -- 能否进入（申请完房间后记录）
        middleFamilyResult = 0,                                     -- 0失败，1-8为排名
        taoyuanValue = 0,                                           -- 桃园结义成长值
        taoyuanVersion = 0,                                         -- 桃园结义版本（满了后递增）
  }
  return family
end
-- 军团创建
function create(human, familyName, lv)
    if familyName2Family[familyName] then
        assert()
    end
    local family = familyTemplate()
    family.familyName = familyName
    family.familyLv = lv
    family.zhandouli = human.db.zhandouli
    LuaMongo.insert(DB.db_family, family)
    uuid2Family[family._id] = family
    familyName2Family[family.familyName] = family
    addMember(family, human.db, FamilyDefine.OFFICIAL_LEADER, human)
    delApplys(human._id)
    return true
end

-- 军团销毁
function destory(family, uuid)
    if not family._id or not uuid2Family[family._id] or not familyName2Family[family.familyName] then
        assert()
    end

    FamilyLogicFW.allyBreak(family)

    uuid2Family[family._id] = nil
    familyName2Family[family.familyName] = nil
    if uuid then
        onQuitFamilyCB(uuid)
    end
    DBRemove._id = family._id
    LuaMongo.remove(DB.db_family, DBRemove)
    return true
end

-- 军团更新
function update(family)
    if not family._id then
        assert()
    end
    DBUpdate._id = family._id
    LuaMongo.update(DB.db_family, DBUpdate, family)
end

-- 加成员
function addMember(family, db, official, human)
    if not family._id then
        assert()
    end
    local uuid
    local blue
    if not human then
        human = Human.onlineUuid[db._id]
        if human then
            db = human.db
        end
        uuid = RoleFFI.ffi_string(db._id)
        blue = ""
    else
        uuid = human._id
        local bluelv = 0
        if human.db.blueVip ~=0 then
            bluelv = human.db.blueVipLv
        end 
        blue = string.format("%d%d%d", bluelv, human.db.blueYearVip, human.db.blueSuperVip) 
    end

    if #family.memberList >= getMemberMaxCnt(family.familyLv) then
        return
    end

    db.familyUuid = family._id
    db.familyName = family.familyName
    db.familyLv = family.familyLv
    db.familyWarResult = family.middleFamilyResult
    db.familyBanner = family.familyBanner
    RankHandler.familyChange(RoleFFI.ffi_string(db._id))

    if not human then
        nUpdateSet = {}
        nUpdateSet._id = uuid
        nUpdateSet.familyUuid = family._id
        nUpdateSet.familyName = family.familyName
        RoleDBLogic.saveRoleSset(nUpdateSet)
    else
        if human.familyUuid == "" then
            human.familyUuid = family._id
            human.familyName = family.familyName
        end
    end

    family.memberList[#family.memberList + 1] = {
        uuid = uuid,
        name = RoleFFI.ffi_string(db.name),
        head = db.head,
        lv = db.lv,
        sex = db.sex,
        official = official,
        lastLoginTime = db.lastLoginTime,
        lastLogOutTime = 0,
        zhandouli = db.zhandouli,
        familyContribution = db.familyContribution,       --帮会贡献
        familyContributionSum = 0,
        fwar = nil, --是否参战
        blue = blue,  --蓝钻串
    }

    DBUpdate._id = family._id
    LuaMongo.update(DB.db_family, DBUpdate, family)
    onEnterFamilyCB(db._id)

    return true
end

-- 删成员
function delMember(family, targetUuid)
    if not family._id then
        assert()
    end

    local memberLen = #family.memberList
    for index, member in ipairs(family.memberList) do
        if member.uuid == targetUuid then
            family.memberList[index], family.memberList[#family.memberList] = family.memberList[#family.memberList], family.memberList[index]
            family.memberList[#family.memberList] = nil
            DBUpdate._id = family._id
            LuaMongo.update(DB.db_family, DBUpdate, family)
            local target = Human.onlineUuid[targetUuid]
            if target then
                --Human.doCalc(target)                
            end
            if memberLen > 1 then
                onQuitFamilyCB(targetUuid, family)
            end
            return
        end
    end
    assert(nil, family._id, targetUuid)
end

-- 加军团仓库记录
function addStoreRecord(family, human, grid, type)
    family.storeRecord[#family.storeRecord + 1] = {}
    local record = family.storeRecord[#family.storeRecord]
    record.grid = grid 
    record.time = os.time()
    record.type = type
    record.name = human.name
    record.uuid = human._id
    local cap = FamilyDefine.FAMILY_STORE_RECORD_MAX_CNT
    if #family.storeRecord > cap then
        for i = 1, cap do
            family.storeRecord[i] = family.storeRecord[i + #family.storeRecord - cap]
        end
        for i = cap + 1, #family.storeRecord do
            family.storeRecord[i] = nil
        end
    end
    return record
end

--取军团在线玩家fds
function getOnlineFds(family, withOutUuid)
    local fds = Msg.list
    local len = 0
    for _, member in ipairs(family.memberList) do
        if member.uuid ~= withOutUuid then
            local target = Human.onlineUuid[member.uuid]
            if target and target.fd then
                target.db.familyLv = family.familyLv
                target.db.familyBanner = family.familyBanner
                len = len + 1
                fds[len] = target.fd
            end
        end
    end
    fds[0] = len
    return fds
end

function changeBlue(human)
    local family = findByUuid(RoleFFI.ffi_string(human.db.familyUuid))
    if not family then
        return
    end

    local member = getMember(family, human._id)
    if not member then
        human.db.familyUuid = ""
        return
    else
        if human.db.blueVip ~=0 then
          member.blue = string.format("%d%d%d", human.db.blueVipLv, human.db.blueYearVip, human.db.blueSuperVip) 
        else
          member.blue = nil
        end
    end
end
-- 玩家登录
function onUserLogin(human)
    local family = findByUuid(human.familyUuid)
    if not family then
        human.db.familyUuid = ""
        human.db.familyName = ""
        if not Config.IS_MIDDLE then
            human.db.familyWarResult = 0
        end
        human.familyUuid = ""
        human.familyName = ""
        return
    end

    local member = getMember(family, human._id)
    if not member then
        human.db.familyUuid = ""
        human.db.familyName = ""
        human.db.familyWarResult = 0
        human.familyUuid = ""
        human.familyName = ""
        return
    end
   

    --checkAddActivityRate(family)
    member.lastLoginTime = human.db.lastLoginTime
    member.zhandouli = human.db.zhandouli
    member.lv = human.db.lv
    if human.db.blueVip ~=0 then
      member.blue = string.format("%d%d%d", human.db.blueVipLv, human.db.blueYearVip, human.db.blueSuperVip) 
    else
      member.blue = nil
    end
    FamilyLogicFW.memberUpdate(family, member)
    family.zhandouli = getZhandouli(family)
    update(family)

    -- check reward cnt
    --if Util.isTimeOver(human.db.familyRewardGetTime, 4) then
    --    human.db.familyRewardGetTime = nil
    --    human.db.familyRewardGetCnt = nil
    --end

    -- check apply cnt
    if isOfficial(family, FamilyDefine.OFFICIAL_LEADER, human._id)
        or isOfficial(family, FamilyDefine.OFFICIAL_FU_LEADER, human._id)
        or isOfficial(family, FamilyDefine.OFFICIAL_ZHANGLAO, human._id) then

        local msgRet = Msg.gc.GC_FAMILY_APPLY_CNT_CHANGE
        msgRet.cnt = getApplyListLen(family)
        Msg.send(msgRet, human.fd)
    end
    if family.sign ~= "" then
        Broadcast.sendBroadcast(human, Lang.FAMILY, string.format(Lang.FAMILY_SIGN, family.sign))
    end
    --登陆处理桃园结义
    if family.taoyuanVersion > 0 then
      if human.db.taoyuanVersion == 0 then 
        human.db.taoyuanVersion = family.taoyuanVersion
        for i = 0, 4 do
            human.db.taoyuanReward[i] = 1
        end
      elseif family.taoyuanVersion > human.db.taoyuanVersion then
        local ok = true
        for i = 0, 4 do
            if human.db.taoyuanReward[i] == 1 then
                ok = nil
                break
            end
        end
        if ok then
            human.db.taoyuanVersion = family.taoyuanVersion
            for i = 0, 4 do
                human.db.taoyuanReward[i] = 1
            end
        end
      end
    elseif human.db.taoyuanVersion == 0 then
      for i = 0, 4 do
            human.db.taoyuanReward[i] = 0
      end
    end
    
end

--玩家登出
function onUserLoginOut(human)
    delInvites(human.name)

    local family = findByUuid(RoleFFI.ffi_string(human.db.familyUuid))
    if not family then
        return
    end

    local member = getMember(family, human._id)
    if not member then
        human.db.familyUuid = ""
        return
    else
        member.lastLogOutTime = os.time()
        member.zhandouli = human.db.zhandouli
        member.lv = human.db.lv
        family.zhandouli = getZhandouli(family)
    end
    FamilyLogicFW.memberUpdate(family, member)
    FamilyLogicFW.onRefuseByLoginOut(human)
    update(family)
end

--玩家保存
function onUserSave(human)
    local family = findByUuid(RoleFFI.ffi_string(human.db.familyUuid))
    if not family then
        return
    end

    local member = getMember(family, human._id)
    if not member then
        human.db.familyUuid = ""
        return
    end
    member.zhandouli = human.db.zhandouli
    member.lv = human.db.lv
    family.zhandouli = getZhandouli(family)
    update(family)
end

-- check 军团邀请列表
function checkFamilyInviteList()
    local now = os.time()
    for _, family in pairs(uuid2Family) do
        if family.inviteList then
            for uuid, time in pairs(family.inviteList) do
                if now - time >= 600 then -- 10min
                    family.inviteList[uuid] = nil
                end
            end
        end
        update(family)
    end
end

-- check 军团称号
function checkFamilyTitleList()
    for k in pairs(uuid2Family) do
    end
end

--check add rate
function checkAddActivityRate(family)
    local now = os.time()
    if not family.activityRateTime or not Util.isSameDay(family.activityRateTime) then
        family.activityRateTime = now
        family.activityRate = family.activityRate + FamilyDefine.ACTIVITY_PER_RATE
        if family.activityRate >= FamilyDefine.ACTIVITY_RATE then
            family.activityRate = FamilyDefine.ACTIVITY_RATE
        end
    end
end

function isMemberOnline(family)
    local onlineUuid = Human.onlineUuid
    for _, member in ipairs(family.memberList) do
        if onlineUuid[member.uuid] then
            return true
        end
    end
end

--军团成员最大值
function getMemberMaxCnt(familyLv)
    return BuildingExcel[familyLv].memberMaxCnt
end

-- 进入军团回调

function onEnterFamilyCB(uuid)

end

-- 退出军团回调
function onQuitFamilyCB(uuid, family)
  CopyCallbackW.onQuitFamilyCB(family, uuid)
end

function checkCanAdd(cb, fn)
    for k, v in ipairs(cb) do
        if v == fn then
            return
        end
    end
    return true
end



-- 改名 或 变性
function onNameChange(human)

    local family = findByUuid(human.db.familyUuid)
    if not family then
        human.db.familyUuid = nil
        return
    end

    local member = getMember(family, human._id)
    if not member then
        human.db.familyUuid = nil
        return
    end

    member.zhandouli = human.db.zhandouli
    member.lv = human.db.lv
    member.name = human.db.name
    member.head = human.db.head
    member.sex = human.db.sex

    FamilyLogicFW.memberUpdate(family, member)
    family.zhandouli = getZhandouli(family)
    update(family)

end

function init()
    LuaMongo.find(DB.db_family)
    local invalidFamilyUuids = {}
    while true do
        local tmp = familyTemplate()
        if not LuaMongo.next(DB.db_family, tmp) then
            break
        end
        local leader = getLeader(tmp)
        if leader then
            uuid2Family[tmp._id] = tmp
            familyName2Family[tmp.familyName] = tmp
        else
            invalidFamilyUuids[#invalidFamilyUuids + 1] = tmp._id
        end
    end

    -- del invalid family
    for _, uuid in ipairs(invalidFamilyUuids) do
        DBRemove._id = uuid
        LuaMongo.remove(DB.db_family, DBRemove)
    end

    checkFamilyInviteList()
    checkFamilyTitleList()    
    updateDaily()
end

function sendFamily(mm, family)
    local list = Msg.list
    local len = 0
    for k, v in ipairs(family.memberList) do
        local human = Human.onlineUuid[v.uuid]
        if human and human.fd then
            len = len + 1
            list[len] = human.fd
        end
    end
    if 0 < len then
        list[0] = len
        Msg.sendMulti(mm, list)
    end
end
--
-- 类似于Human.updateDaily(human)
function updateDaily(must)
    local os_time = os.time()
    for _, family in pairs(uuid2Family) do
      if must or not family.update_daily_time or not Util.isSameDay(family.update_daily_time) then
        family.update_daily_time = os_time
        --跨天更新的在这里处理
        family.familyCopyTimes = 0   
        
        update(family) 
      end
    end
end

function addCopyTimes(family)
   family.familyCopyTimes = family.familyCopyTimes + 1
   if family.familyCopyTimes > 2 then
      family.familyCopyTimes = 2
   end
   update(family)
end

function saveAll()
  for _, family in pairs(uuid2Family) do
      update(family)
  end
end

function getFamilyLvByFamilyUuid(familyUuid)
    local family = findByUuid(familyUuid)
    if family then
        return family.familyLv
    end
    return 0
end

function getFamilyBannerByFamilyUuid(familyUuid)
    local family = findByUuid(familyUuid)
    if family then
        return family.familyBanner
    end
    return 0
end


